package controller;


import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import util.JDBCUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Controller
public class UserController {
    @RequestMapping("/regUser")
    public void reg(User user, HttpServletResponse response){
        System.out.println("开始处理注册!");
        System.out.println("User"+user);
        String user_name = user.getName();
        String password = user.getPassword();
        if(user_name==null||user_name.isEmpty()||
        password==null||password.isEmpty()){
            try {
                response.sendRedirect("/reg_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        System.out.println("user_name:"+user_name+"password:"+password);

        try (Connection connection = JDBCUtil.getConnection();){
            String sql = "select 1 from userinfo where name=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user_name);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                response.sendRedirect("/have_user.html");
                return;
            }
            sql = "insert into userinfo (name,password) values(?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user_name);
            ps.setString(2,password);
            int i = ps.executeUpdate();
            if(i>0){
                response.sendRedirect("/reg_success.html");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //开始处理登录
    @RequestMapping("/loginUser")
        public void login(HttpServletResponse response, HttpServletRequest request){
            System.out.println("开始处理登录");
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            if(name==null||name.trim().isEmpty()||
                    password==null||password.trim().isEmpty()) {
                try {
                    response.sendRedirect("/login_info_error");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }
            try(Connection connection = JDBCUtil.getConnection();){
                String sql = "SELECT id,name,password " +
                             "FROM userinfo " +
                             "WHERE name=? " +
                             "AND password=?";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,password);
                ResultSet resultSet = preparedStatement.executeQuery();
                if(resultSet.next()){
                    response.sendRedirect("/login_success.html");
                }else{
                    response.sendRedirect("/login_fail.html");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        @RequestMapping("/User_shan")
        public void user_shan (HttpServletResponse response,HttpServletRequest request){
            String name = request.getParameter("name");
            if(name==null||name.trim().isEmpty()){
                try {
                    response.sendRedirect("/user_fail.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }
            try(Connection connection = JDBCUtil.getConnection();){
                System.out.println("开始处理删除功能");
                String sql = "delete from userinfo where name=?";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,name);
                int i = preparedStatement.executeUpdate();
                if(i>0){
                    response.sendRedirect("delete.html");
                }else{
                    response.sendRedirect("delete_fail");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        @RequestMapping("/User_gai")
        public void user_gai (HttpServletResponse response,HttpServletRequest request){
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            if(name==null||name.trim().isEmpty()||
            password==null||password.trim().isEmpty()){
                try {
                    response.sendRedirect("/user_fail.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }
            try(Connection connection = JDBCUtil.getConnection();){
                String sql = "update userinfo set password=? where name=?";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,password);
                preparedStatement.setString(2,name);
                int i = preparedStatement.executeUpdate();
                if(i>0){
                    response.sendRedirect("/update.html");
                }else{
                    response.sendRedirect("/update_fail.html");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理动态页面!!!!!");
        /*
            1:从数据库中将所有用户信息查询出来
            2:将每个用户信息体现到html页面的表中的一行中
         */
        try(
                Connection connection = JDBCUtil.getConnection();
        ){
            String sql = "SELECT id,name,password FROM userinfo";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            //在response.getWriter()之前设置响应头，告知浏览器正文类型和字符集，避免页面乱码
            response.setContentType("text/html;charset=utf-8");
            //通过response.getWriter()获取的缓冲字符流写出的内容会作为正文发送给浏览器
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("    <center>");
            pw.println("        <h1>用户列表</h1>");
            pw.println("        <table border=\"1\">");
            pw.println("            <tr>");
            pw.println("                <td>ID</td>");
            pw.println("                <td>用户名</td>");
            pw.println("                <td>密码</td>");
            pw.println("            </tr>");
            while(rs.next()) {
                int id = rs.getInt("id");
                pw.println("            <tr>");
                pw.println("                <td>"+id+"</td>");
                pw.println("                <td><a href='/articleList?userId="+id+"' >"+rs.getString("name")+"</a></td>");
                pw.println("                <td>"+rs.getString("password")+"</td>");
                pw.println("            </tr>");
            }
            pw.println("        </table>");
            pw.println("    </center>");
            pw.println("</body>");
            pw.println("</html>");

            rs.close();//结果集释放
            ps.close();//执行计划释放

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }


    }


}
